package ch04;

/**
 * Created by 其 on 2016/3/10.
 */
public class Shuffle{
    public static void main(String[] args){
        String[] pokers = {
                "梅1","梅2","梅3","梅4","梅5","梅6","梅7","梅8","梅9","梅10","梅J","梅Q","梅K",
                "砖1","砖2","砖3","砖4","砖5","砖6","砖7","砖8","砖9","砖10","砖J","砖Q","砖K",
                "桃1","桃2","桃3","桃4","桃5","桃6","桃7","桃8","桃9","桃10","桃J","桃Q","桃K",
                "心1","心2","心3","心4","心5","心6","心7","心8","心9","心10","心J","心Q","心K"
        };
        //定义一个0-51的随机数（不重复）数组。
        int[] numbers = new int[pokers.length];
        //赋初值，简化判断是否有重复随机数的循环                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   次数。
        for(int i=0; i<pokers.length; i++){
            numbers[i] = -1;
        }
        int num;
        boolean flag;
        for(int i=0; i<pokers.length; i++){
            //产生随机数。
            while(true){
                flag = true;
                num = (int) (Math.random() * pokers.length);
                //凡是搜索到了-1就表示已经搜索完毕。
                for(int j=0; numbers[j]!=-1 ;j++){
                    if(numbers[j] == num){
                        flag = false;
                        break;
                    }
                }
                //不重复，就向随机数数组中赋值。
                if(flag){
                    numbers[i] = num;
                    break;
                }
            }
        }
	/*
		调代码时用到，用以判断是否成功生成了0-51的不重复随机数。
		for(int number : numbers){
			System.out.print(number + " ");
		}
		System.out.println();
	*/
        for(int i=0; i<pokers.length; i++){
            System.out.printf("%-4s",pokers[numbers[i]]);
            if((i+1)%13 == 0)
                System.out.println();
        }
    }
}